<?php
/**
 * 考勤系统 职位分组相关
 */
namespace app\attendance\controller;

use app\attendance\Base;
use think\Config;
use think\Db;
use think\Loader;
use think\exception\HttpException;
use baseutils\BaseUtil; # 验证类

use com\gdl\kq\interfaces\AttAdminPositionServiceClient;
use com\gdl\kq\dataobject\adminposition\PositionDTO; 
use com\gdl\kq\dataobject\adminposition\PositionInfoDTO; 


class AdminPosition extends Base
{
	
	//分级带职位分组的列表
	public function getPositionGroup()
    {
    	$msg = ['msg'=>'暂无数据','code'=>'400','data'=>[]];

		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ""; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

		#其他参数
		$liekName = $this->request->param('title') ? getStr($this->request->param('title')) : '';  
		$offset = $this->request->param('offset') ? getStr($this->request->param('offset')) : ''; #分页
		$num = $this->request->param('num') ? getStr($this->request->param('num')) : 10; #条数

		#其他参数
		try {
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionDTO();
			$sdkData = ['platformId'=>$platformId, 'unitsId'=>$unitId, 'positionName'=>$liekName];
			$positionDO->offset = $offset; 
			$positionDO->num = $num; 
			$positionDO->data = $sdkData;

			$result = $api->getPositionGroup($positionDO);
			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code;
				if($result->code == 200 && !empty($result->data)){
					
					$msg['data'] = json_decode($result->data[0]['list']);
				} 
			}

		} catch (Exception $ex) {
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);	 
	}

	// 纯职位列表
	public function getPositionList(){
		$msg = ['msg'=>'暂无数据','code'=>'400','data'=>[]];

		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

		#其他参数
		$liekName = $this->request->param('title') ? getStr($this->request->param('title')) : ''; # 职位名称
		$groupId = $this->request->param('groupId') ? getStr($this->request->param('groupId')) : ''; # 分组id
		
		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionDTO();
			$sdkData = ['platformId'=>$platformId, 'unitsId'=>$unitId, 'positionName'=>$liekName, 'groupId'=>$groupId];
			$positionDO->data = $sdkData;
			$result = $api->getPositionList($positionDO);

			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code;
				if(!empty($result->data)){
					$msg['data'] = $result->data;
				}
			} 
		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}

	//添加编辑分组
	public function editGroup(){
		$msg = ['msg'=>'编辑失败','code'=>'400','data'=>[]];

		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

		#其他参数
		$id = $this->request->param('id') ? getStr($this->request->param('id')) : ""; #分组id
		$title = $this->request->param('title') ? getStr($this->request->param('title')) : ""; #分组名 多个用逗号分隔
		$sort = $this->request->param('sort') ? getStr($this->request->param('sort')) : 0; #排序
		$type = $this->request->param('type') ? getStr($this->request->param('type')) : "add"; #类型 add 添加 edit修改

		# 验证
		if(empty($title)){$msg['msg'] = "分组名不能为空"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionDTO();
			$sdkData = ['id'=>$id,'title'=>$title,'sort'=>$sort, 'type'=>$type, 'platformId'=>$platformId, 'unitId'=>$unitId];

			$positionDO->type = $type;
			$positionDO->data = $sdkData;
			$result = $api->editGroup($positionDO);
			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code;
				$msg['data'] = $result->data;
			}
		}catch(Exception $e){
			$msg['msg'] = $e->getMessage();
		}
		return json_encode($msg);
	}

	//添加编辑职位
	public function editPosition(){
		$msg = ['msg'=>'编辑失败','code'=>'400','data'=>[]];

		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

		#其他参数
		$id = $this->request->param('id') ? getStr($this->request->param('id')) : ""; #职位id
		$title = $this->request->param('title') ? getStr($this->request->param('title')) : ""; #职位名多个用逗号分隔
		$groupId = $this->request->param('groupId') ? getStr($this->request->param('groupId')) : 0; #分组id
		$sort = $this->request->param('sort') ? getStr($this->request->param('sort')) : 0; #排序
		$type = $this->request->param('type') ? getStr($this->request->param('type')) : "add"; #类型 add 添加 edit修改

		# 验证
		if(empty($title)){$msg['msg'] = "职位名不能为空"; return json_encode($msg);}
		if($type=='add'){
			if(empty($groupId)){$msg['msg'] = "请选择职位分组"; return json_encode($msg);} # 这里必须选分组才能添加职位
		}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionDTO();
			$sdkData = ['id'=>$id,'title'=>$title,'sort'=>$sort, 'type'=>$type, 'platformId'=>$platformId, 'unitId'=>$unitId,'groupId'=>$groupId];

			$positionDO->type = $type;
			$positionDO->data = $sdkData;
			$result = $api->editPosition($positionDO);
			if($result->success == true){
				$msg['msg'] = $result->message;
				$msg['code'] = $result->code;
				$msg['data'] = $result->data;
			}
		}catch(Exception $e){
			$msg['msg'] = $e->getMessage();
		}
		return json_encode($msg);
	}

	//删除分组 单个删除 批量删除
	public function deleteGroup(){
		$msg = ['msg'=>'删除失败','code'=>'400','data'=>[]];

		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}
		
		#其他参数
		$groupId = $this->request->param('groupId') ? getStr($this->request->param('groupId')) : ''; #多个用逗号分隔

		#处理去除，，，
		$myGroupId = explode(",", $groupId);
		$idArray = [];
		foreach($myGroupId as $v){
			if(!empty($v)){
				$idArray[] = $v;
			}
		}
		$groupId = implode(",", $idArray);

		#验证
		if(empty($groupId)){$msg['msg'] = "请选择需要删除的分组"; json_encode($msg);}

		try{
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionInfoDTO();
			$positionDO->idstr = $groupId;
			$result = $api->deleteGroup($positionDO);

			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				$msg['data'] = $result->data; 
			}
		}catch(Exception $e){
			$msg['msg'] = $e->getMessage();
		}

		return json_encode($msg);
	}

	//删除职位 单个删除 批量删除
	public function deletePosition(){
		$msg = ['msg'=>'删除失败','code'=>'400','data'=>[]];

		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}
		
		#其他参数
		$positionId = $this->request->param('positionId') ? getStr($this->request->param('positionId')) : ''; #多个用逗号分隔

		#处理去除，，，
		$myGroupId = explode(",", $positionId);
		$idArray = [];
		foreach($myGroupId as $v){
			if(!empty($v)){
				$idArray[] = $v;
			}
		}
		$positionId = implode(",", $idArray);

		#验证
		if(empty($positionId)){$msg['msg'] = "请选择需要删除的职位"; json_encode($msg);}

		try{
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionInfoDTO();
			$positionDO->idstr = $positionId;
			$result = $api->deletePosition($positionDO);

			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				$msg['data'] = $result->data; 
			}
		}catch(Exception $e){
			$msg['msg'] = $e->getMessage();
		}

		return json_encode($msg);
	}

	//分组详情
	public function groupInfo(){

		$msg = ['msg'=>'获取失败','code'=>'400','data'=>[]];
		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

		#其他参数
		$id = $this->request->param('id') ? $this->request->param('id') : "";

		#验证
		if(empty($id)){$msg['msg'] = "请选择分组"; return json_encode($msg);}

		try{
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionInfoDTO();
			$positionDO->id = $id;
			$result = $api->groupInfo($positionDO);

			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				if(!empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['list']); 	
				}
			}
		}catch(Exception $ex){
			$msg['msg']= $ex->getMessage();
		}
		return json_encode($msg);
	}

	//职位详情
	public function positionInfo(){

		$msg = ['msg'=>'获取失败','code'=>'400','data'=>[]];
		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

		#其他参数
		$id = $this->request->param('id') ? $this->request->param('id') : "1";

		#验证
		if(empty($id)){$msg['msg'] = "请选择职位"; return json_encode($msg);}

		try{
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionInfoDTO();
			$positionDO->id = $id;
			$result = $api->positionInfo($positionDO);

			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				if(!empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['list']); 	
				}
			}
		}catch(Exception $ex){
			$msg['msg']= $ex->getMessage();
		}
		return json_encode($msg);
	}

	//分组列表
	public function myPositionList(){

		$msg = ['msg'=>'获取失败','code'=>'400','data'=>[]];
		# 公共参数
		$adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
		$adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
		$unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0; #单位id
		if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

		try{
			# 实例化
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAdminPositionServiceClient(NULL);
			\ApiClient::build($api); 
			$positionDO = new PositionInfoDTO(); 
			$sdkData = ['platformId'=>$platformId, 'unitsId'=>$unitId];
			$positionDO->idarr = $sdkData; 
			$result = $api->myPositionList($positionDO);

			if($result->success ==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				$msg['data'] = $result->data; 
			}
		}catch(Exception $ex){
			$msg['msg']= $ex->getMessage();
		}
		return json_encode($msg);
	}

	 
	 
}
